function Promise(callback) {
    let that = this

    this.arrs = []
    // 默认状态
    this.PromiseState = 'pending'
    // 默认结果值
    this.PromiseResult = undefined

    // 成功的函数
    function resolve(velue) {
        if (that.PromiseState != 'pending') return
        // 成功的状态
        that.PromiseState = 'fulfilled'
        // 成功的结果值
        that.PromiseResult = velue

        if (that.arrs.length > 0) {
            setTimeout(() => {
                that.arrs.forEach(item => {
                    item.onResolved(velue)
                })
            })
        }
    }
    // 失败的函数
    function reject(reason) {
        if (that.PromiseState != 'pending') return
        // 失败的状态
        that.PromiseState = 'rejected'
        // 失败的结果值
        that.PromiseResult = reason

        if (that.arrs.length > 0) {
            setTimeout(() => {
                that.arrs.forEach(item => {
                    item.onRejected(reason)
                })
            })
        }
    }

    callback(resolve, reject)
}
Promise.prototype.then = function (onResolved, onRejected) {

    if (typeof onRejected != 'function') {
        onRejected = reason => {
            throw reason
        }
    }

    if (typeof onResolved != 'function') {
        onResolved = velue => {
            return velue
        }
    }
    return new Promise((resolve, reject) => {

        if (this.PromiseState === 'fulfilled') {
            setTimeout(()=>{
                try {
                    let res = onResolved(this.PromiseResult)
                    if (res instanceof Promise) {
                        res.then(resolve, reject)
                    } else {
                        resolve(res)
                    }
                } catch (e) {
                    reject(e)
                }
            })
        } else if (this.PromiseState === 'rejected') {
            setTimeout(()=>{
                try {
                    let res = onRejected(this.PromiseResult)
                    if (res instanceof Promise) {
                        res.then(resolve, reject)
                    } else {
                        reject(res)
                    }
                } catch (e) {
                    reject(e)
                }
            })
        } else {
            this.arrs.push({
                onResolved: function (velue) {
                    try {
                        let res = onResolved(velue)
                        if (res instanceof Promise) {
                            res.then(resolve, reject)
                        } else {
                            resolve(res)
                        }
                    } catch (e) {
                        reject(e)
                    }
                },
                onRejected: function (reason) {
                    try {
                        let res = onRejected(reason)
                        if (res instanceof Promise) {
                            res.then(resolve, reject)
                        } else {
                            reject(res)
                        }
                    } catch (e) {
                        reject(e)
                    }
                }
            })
        }
    })
}
Promise.prototype.catch = function (onRejected) {
    return this.then(undefined, onRejected)
}